var initPhotoSwipeDefault = function(cb,initCb,items) {


    // parse picture index and gallery index from URL (#&pid=1&gid=2)
    var photoswipeParseHash = function() {
        //var hash = window.location.hash.substring(1),
        //    params = {};
        //
        //
        //if(hash.length < 5) {
        //    return params;
        //}
        //
        //var vars = hash.split('&');
        //
        //for (var i = 0; i < vars.length; i++) {
        //    if(!vars[i]) {
        //        continue;
        //    }
        //    var pair = vars[i].split('=');
        //    if(pair.length < 2) {
        //        continue;
        //    }
        //    params[pair[0]] = pair[1];
        //}
        //
        ////if(params.gid) {
        ////    params.gid = parseInt(params.gid, 10);
        ////}
        //
        //return params;

        //var hash = window.location.href.substring(parseInt(window.location.href.lastIndexOf('/')+1)).split('_')[1];
        var hash = window.location.href.substring(parseInt(window.location.href.lastIndexOf('/')+1));
        hash = hash.substring(parseInt(hash.indexOf('_')+1));
        var params = {};
        //if(hash.length < 5) {
        //    return params;
        //}
        //
        //var vars = hash.split('&');
        //
        //for (var i = 0; i < vars.length; i++) {
        //    if(!vars[i]) {
        //        continue;
        //    }
        //    var pair = vars[i].split('=');
        //    if(pair.length < 2) {
        //        continue;
        //    }
        //    params[pair[0]] = pair[1];
        //}
        //
        ////if(params.gid) {
        ////    params.gid = parseInt(params.gid, 10);
        ////}
        //
        //return params;

        params.gid = hash.split('_')[0];
        params.pid = hash.split('_')[1];

        return params;
    };


    var openPhotoSwipe = function(index, disableAnimation, fromURL ,items) {
        var pswpElement = document.querySelectorAll('.pswp')[0],
            gallery,
            options;

        //items = parseThumbnailElements(galleryElement);

        //items = JSON.parse(photoItems);
        //console.log('items',items);
        items = items;

        // define options (if needed)
        options = {

            // define gallery index (for URL)
            //galleryUID: galleryElement.getAttribute('data-pswp-uid'),

            //getThumbBoundsFn: function(index) {
            //    // See Options -> getThumbBoundsFn section of documentation for more info
            //    var thumbnail = items[index].el.getElementsByTagName('img')[0], // find thumbnail
            //        pageYScroll = window.pageYOffset || document.documentElement.scrollTop,
            //        rect = thumbnail.getBoundingClientRect();
            //
            //    return {x:rect.left, y:rect.top + pageYScroll, w:rect.width};
            //},
            galleryUID:1,
            galleryPIDs: true,
            history: true,
            closeEl:false,
            shareEl:false,
            zoomEl:false,
            loop: false,
            showAnimationDuration: 0,
            hideAnimationDuration: 0,
            pinchToClose:false,

            closeOnVerticalDrag: false,
            //clickToCloseNonZoomable: false,
            fullscreenEl: false,
            arrowEl: true,
            preloaderEl: true,
            tapToClose: false,
            spacing:0
        };

        // PhotoSwipe opened from URL
        if(fromURL) {
            if(options.galleryPIDs) {
                // parse real index when custom PIDs are used
                // http://photoswipe.com/documentation/faq.html#custom-pid-in-url
                for(var j = 0; j < items.length; j++) {
                    if(items[j].pid == index) {
                        options.index = j;
                        break;
                    }
                }
            } else {
                // in URL indexes start from 1
                options.index = parseInt(index, 10) - 1;
            }
        } else {
            options.index = parseInt(index, 10);
        }

        // exit if index not found
        if( isNaN(options.index) ) {
            return;
        }

        if(disableAnimation) {
            options.showAnimationDuration = 0;
        }

        // Pass data to PhotoSwipe and initialize it
        gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
        gallery.init();

        initCb && initCb(gallery);
        gallery.listen('afterChange',function(){
            cb && cb();
            initCb && initCb(gallery);
        });
    };

    // Parse URL and open gallery if it contains #&pid=3&gid=1
    var hashData = photoswipeParseHash();
    if(hashData.pid) {
        //openPhotoSwipe( hashData.pid , true, true );
        $(document).on('click',function(){
            openPhotoSwipe( hashData.pid , true, true ,items);
        });
        $(window).on('resize',function(){
            openPhotoSwipe( hashData.pid , true, true ,items);
        })
    }
};
